package util;

import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.Property;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import java.util.LinkedList;
import java.util.List;

public class DF {
	static public void deregister(Agent a) {
		try {
			DFService.deregister(a);
		} catch (FIPAException ex) {
			
		}
	}

	static public void register(Agent a, String name, String serviceType) throws FIPAException {
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(a.getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setName(name);
		sd.setType(serviceType);
		dfd.addServices(sd);

		DFService.register(a, dfd);
	}

	static public void register(Agent a, String name, String serviceType, List<Property> properties) throws FIPAException {
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(a.getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setName(name);
		sd.setType(serviceType);

		for(Property p: properties)
			sd.addProperties(p);

		dfd.addServices(sd);

		DFService.register(a, dfd);
	}

	static public void register(Agent a, String name, String serviceType, Property property) throws FIPAException {
		List<Property> l = new LinkedList<Property>();
		l.add(property);
		register(a, name, serviceType, l);
	}

	static public DFAgentDescription[] search(Agent a, String serviceType) throws FIPAException {
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription templateSd = new ServiceDescription();
		templateSd.setType(serviceType);
		template.addServices(templateSd);

		return DFService.search(a, template, null);
	}

	static public DFAgentDescription[] search(Agent a, String serviceType, List<Property> properties) throws FIPAException {
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription templateSd = new ServiceDescription();
		templateSd.setType(serviceType);

		for(Property p: properties)
			templateSd.addProperties(p);

		template.addServices(templateSd);

		return DFService.search(a, template, null);
	}

	static public DFAgentDescription[] search(Agent a, String serviceType, Property property) throws FIPAException {
		List<Property> l = new LinkedList<Property>();
		l.add(property);
		return search(a, serviceType, l);
	}
}
